/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui.util;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import org.bushe.swing.event.EventBus;

import who.tmap.ui.event.MultiLayerCheckBoxListClicked;

/**
 * A scroll pane containing a JList with a check box for each item
 * @author stl
 *
 */
public class CheckBoxList extends JScrollPane {
	
	private JList dataList;
	private JList checkList;
	private DefaultListModel dataListModel, checkListModel;
	
	public CheckBoxList() {
		super();
		
		initialize();
	}

	private void initialize() {
		
		checkList = getCheckList();

		dataList = getDataList();

    	dataList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    	
		checkList.setCellRenderer( new CheckBoxRenderer() );

		checkList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  
		
		setRowHeaderView( checkList );

		// Now, make the listbox with actual descriptions as the main view
		setViewportView( dataList );
		
		// Align both the checkbox height and widths
		dataList.setFixedCellHeight(20);
		checkList.setFixedCellHeight( dataList.getFixedCellHeight() );
		checkList.setFixedCellWidth(20);
		
	}	
	
	private JList getCheckList() {
		if ( checkList == null ) {
			checkList = new JList( getCheckListModel() );
			
			checkList.addMouseListener( new MouseAdapter() {
				
	      		public void mouseClicked( MouseEvent me ) {
		  			int selectedIndex = checkList.locationToIndex( me.getPoint() );
		  			
					if ( selectedIndex < 0 ) {
						return;
					}
	        		
					CheckBoxItem item = (CheckBoxItem)checkListModel.getElementAt( selectedIndex );
	        		item.setChecked( !item.isChecked() );
		  			dataList.setSelectedIndex(selectedIndex);
	        		checkList.repaint();

	        		EventBus.publish( new MultiLayerCheckBoxListClicked() );
	        		
	      		}
	    	});		
		}
		
		return checkList;
	}
	
	public JList getDataList() {
		if ( dataList == null ) {
			dataList =new JList( getDataListModel() );
		}
		
		return dataList;
	}
	
	private DefaultListModel getDataListModel() {
		if ( dataListModel == null ) {
			dataListModel = new DefaultListModel();
		}
	    
	    return dataListModel;
	 }
	
	private DefaultListModel getCheckListModel() {
		if ( checkListModel == null ) {
			checkListModel = new DefaultListModel();
		}
	    
	    return checkListModel;
	 }	
	
	public void add( Object obj ) {
		dataListModel.addElement(obj);
		
		CheckBoxItem cbi =new CheckBoxItem();
		cbi.setChecked( true );
		
		checkListModel.addElement( cbi );
		
		int idx =dataListModel.getSize();
		
		dataList.setSelectedIndex( idx - 1 );
		dataList.ensureIndexIsVisible( dataList.getSelectedIndex() );
		
	}
	
	public Object getDataAt( int idx ) {
		return dataListModel.elementAt( idx );
	}
	
	public int getSelectedIndex() {
		return dataList.getSelectedIndex();
	}
	
	public void remove( int idx ) {
		dataListModel.remove( idx );
		checkListModel.remove( idx );
		
		if ( idx < dataListModel.size() ) {
			dataList.setSelectedIndex( idx );
		} else {
			dataList.setSelectedIndex( idx - 1 );
		}
		
		dataList.ensureIndexIsVisible( dataList.getSelectedIndex() );
	}
	
	public void move( int srcIdx, int tgtIdx ) {
		Object data =dataListModel.remove( srcIdx );
		Object check =checkListModel.remove( srcIdx );
		
		dataListModel.insertElementAt( data, tgtIdx );
		checkListModel.insertElementAt( check, tgtIdx );
		
		dataList.setSelectedIndex( tgtIdx );
		dataList.ensureIndexIsVisible( tgtIdx );
	}
	
	public int getNbElements() {
		return dataListModel.getSize();
	}
	
	public Object[] elements() {
		return dataListModel.toArray();
	}
	
	public boolean isChecked( int idx ) {
		return ((CheckBoxItem)checkListModel.get( idx )).isChecked();
	}
	
	public void check( int idx, boolean val ) {
		((CheckBoxItem)checkListModel.get( idx )).setChecked( val );
	}
	
	public void removeAll() {
		dataListModel.removeAllElements();
		checkListModel.removeAllElements();
	}
	
}
